/**
 * 
 */
package com.app.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.app.dao.ICornDbDao;
import com.app.service.BaseService;
import com.app.service.ICornDBService;
import com.app.service.vo.CornDbDnaVo;
import com.app.service.vo.DNAVo;

/**
 * @author Administrator
 *
 */
public class CornDBServiceImpl extends BaseService implements ICornDBService {

	/**
	 * 
	 */
	public CornDBServiceImpl() {
		logger.info(this.getClass().getName()+" loader");
	}
	
	private static Logger logger = Logger.getLogger(CornDBServiceImpl.class);
	
	private ICornDbDao corndao;

	@SuppressWarnings("unchecked")
	public void init(){
		logger.info("将所有种子库信息纳入缓存中");
		List<CornDbDnaVo> listvo = this.corndao.getAll();
		Map<String,CornDbDnaVo> idmap = new HashMap<String,CornDbDnaVo>();
		Map<String,CornDbDnaVo> namemap = new HashMap<String,CornDbDnaVo>();
		Map<String,CornDbDnaVo> codermap = new HashMap<String,CornDbDnaVo>();
		for(CornDbDnaVo vo:listvo)
		{
			idmap.put(vo.getId(), vo);
			namemap.put(vo.getDnaName(), vo);
			codermap.put(vo.getDnaCoder(), vo);
		}
		this.getCacheService().saveObject("corn-db-list", listvo);
		this.getCacheService().saveObject("corn-db-idmap", idmap);
		this.getCacheService().saveObject("corn-db-codermap", idmap);
	}
	
	/* (non-Javadoc)
	 * @see com.app.service.ICornDBService#getAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<CornDbDnaVo> getAll() {
		List<CornDbDnaVo> listvo = (List<CornDbDnaVo>) this.getCacheService().getObject("corn-db-list");
		if(null==listvo){
			this.init();
		}
		return (List<CornDbDnaVo>) this.getCacheService().getObject("corn-db-list");
	}

	/* (non-Javadoc)
	 * @see com.app.service.ICornDBService#getCornDbDnaVoById(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public CornDbDnaVo getCornDbDnaVoById(String id) {
		Map<String,CornDbDnaVo> idmap = (Map<String,CornDbDnaVo>) this.getCacheService().getObject("corn-db-idmap");
		return idmap.get(id);
	}

	/* (non-Javadoc)
	 * @see com.app.service.ICornDBService#getDNAVoByPath(java.lang.String)
	 */
	@Override
	public DNAVo getDNAVoByPath(String path) {
		return null;
	}

	public ICornDbDao getCorndao() {
		return corndao;
	}

	public void setCorndao(ICornDbDao corndao) {
		this.corndao = corndao;
	}

	@Override
	public void saveDANVo(CornDbDnaVo vo) {
		this.corndao.saveCornDbDnaVo(vo);
		this.init();
	}

	@Override
	public List<CornDbDnaVo> listCornDbDnaVoByParam(String sql) {
		return this.corndao.getCornDbDnaVoBySql(sql);
	}

	@Override
	public int statCornDbDnaVoByParam(String sql) {
		return this.corndao.statCornDbDnaVoBySql(sql);
	}
	
	
}
